Add adhoc option to control prepared-statement caching#35
Conversation
Default adhoc to true in N1ql.config to preserve current behavior (queries are not cached). When set to false, Couchbase will cache the parameterized query plan, improving performance for repeated queries. The option is forwarded to Couchbase::Options::Query in both n1ql and relation query paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces support for the adhoc query option in Couchbase N1QL queries, defaulting it to true in the configuration and passing it through query options and logging. The review feedback highlights an issue in CouchbaseOrm::N1ql.config where setting a partial configuration overwrites the entire thread-local configuration, causing other default options to be lost. A merge strategy is suggested to preserve default values when only a subset of options is configured.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def self.config(new_config = nil) | ||
| Thread.current['__couchbaseorm_n1ql_config__'] = new_config if new_config | ||
| Thread.current['__couchbaseorm_n1ql_config__'] || { | ||
| scan_consistency: DEFAULT_SCAN_CONSISTENCY | ||
| scan_consistency: DEFAULT_SCAN_CONSISTENCY, | ||
| adhoc: true | ||
| } | ||
| end |
There was a problem hiding this comment.
Currently, if a user configures a single option (e.g., CouchbaseOrm::N1ql.config({ adhoc: false })), the entire thread-local configuration hash is overwritten. This causes other configuration options (like scan_consistency) to be lost and default to nil instead of their default values.
To prevent partial configurations from wiping out other defaults, we should merge the new configuration into the existing thread-local configuration, and always merge the thread-local configuration on top of the default configuration hash.
def self.config(new_config = nil)
if new_config
Thread.current['__couchbaseorm_n1ql_config__'] = (Thread.current['__couchbaseorm_n1ql_config__'] || {}).merge(new_config)
end
{
scan_consistency: DEFAULT_SCAN_CONSISTENCY,
adhoc: true
}.merge(Thread.current['__couchbaseorm_n1ql_config__'] || {})
end
Summary
adhoc: truetoN1ql.configdefault, preserving current behavior (queries are not cached)adhocoption toCouchbase::Options::Queryin both n1ql.rb (run_query) and relation.rb (build_query_options)adhocvalue in debug log messagesDepends on: #34 (Parameterize N1QL queries)
Context
With parameterized queries in place (PR #34), setting
adhoc: falseenables Couchbase to cache query plans for repeated queries. This PR exposes the option without changing the default behavior.Users can opt-in to prepared-statement caching via:
Test plan
adhoc: truein expected messages/test --fail-fast=false🤖 Generated with Claude Code